function getUIJson() {
	var uiid=$('#uiid').val();
	var str;
	$.ajax({
		url : "/index/getUIJson?id="+uiid,
		dataType : 'json',
		async:false,
		success : function(json) {
			str=json;
		}
	})
	return str;
}

function getDataGrid(id,str,tvnum){
	var columns=new Array();
	var fistColumns=new Array();
	var secondColumns=new Array();  
	
	var TV=str.TV[tvnum];
	var EO=TV.EO;
	var _url=str.SERVICE;
	var parm="?eoName="+EO[0].EO_NAME;
	for(i=0;i<EO.length;i++){
		var column={};
		var fistColumn={};
		var secondColumn={};
			if(TV.TV_TITLE_LEVL==1){
				if(EO[i].TITLE_LEAVL=="1"){
				column["title"]=EO[i].ALIAS;
				column["field"]=EO[i].DATA_NAME;
				column["width"]=EO[i].WIDTH;
				columns.push(column);
				}
			}else 
				if(EO[i].TITLE_LEAVL=="1"){
			fistColumn["title"]=EO[i].ALIAS;
			if(EO[i].ROWSPAN=='2'){
				fistColumn["field"]=EO[i].DATA_NAME;
			}
		//	fistColumn["field"]=EO[i].DATA_NAME;
		//	fistColumn["width"]=EO[i].WIDTH;
			if(EO[i].COLSPAN>0)	
			fistColumn["colspan"]=EO[i].COLSPAN;
			if(EO[i].ROWSPAN>0)
			fistColumn["rowspan"]=EO[i].ROWSPAN;
			fistColumns.push(fistColumn);
		}else if(EO[i].TITLE_LEAVL=="2"){
			secondColumn["title"]=EO[i].ALIAS;
			secondColumn["field"]=EO[i].DATA_NAME;
			secondColumn["width"]=EO[i].WIDTH;
		if(EO[i].COLSPAN>0)	
			secondColumn["colspan"]=EO[i].COLSPAN;
		if(EO[i].ROWSPAN>0)
			secondColumn["rowspan"]=EO[i].ROWSPAN;
			secondColumns.push(secondColumn);
		}
	}
	var URL=str.service
	var parm=TV.TVEONAME1;
	if(TV.TV_TITLE_LEVL==1){
	$('#'+id+'').datagrid({
	url:_url+"?"+parm,
	columns:[
	columns
	],pagination:true,
	rownumbers:true
	});
	}else{
		$('#'+id+'').datagrid({
			url:_url+"?"+parm,
			columns:[
			     fistColumns,secondColumns
			],pagination:true,
			rownumbers:true,
			frozenColumns:true
			});
	}
	var p=$('#'+id+'').datagrid();
	$(p).pagination({
		pageSize:10,
		pageList:[10,20],
		beforePageText:'第',
		afterPageText:'页',
		displayMsg:'当前显示{from}-{to}条记录 共{total}条记录',
		onBeforeRefresh:function(){
			$(this).pagination('loading');
		}
		
	});
}  
function createTV(str){
	var inhtm="";
	var thvar="<tr>";
	var TV=str.TV;
	console.log(TV.length);
	if(TV.length==2){
		var westhtm="<table id='westtable' fit='true'></table>";
		var easthtm="<table  id='easttable'fit='true'></table>";
		inhtm+="<div class=\"easyui-layout\" data-options=\"fit:true\">";
		inhtm+="<div data-options=\"region:'west',split:true \"  style=\"width:50%;padding:10px;\">"+westhtm+"</div>"
		inhtm+="<div data-options=\"region:'east',split:true,title:'East'\"  style=\"width:50%;padding:10px;\">"+easthtm+"</div>";
		inhtm+="</div>";
		$('#cen1').html(inhtm);
		getDataGrid("westtable",str,0);
		getDataGrid("easttable",str,1);
	}
	if(TV.length==1){
		var inhtm="<table id='centertable'fit='true' ></table>";
		//inhtm+="<div class='easyui-layout' fit='true'>"+centerhtml+"</div>";
		$('#cen1').html(inhtm);
		getDataGrid("centertable",str,0);
	
	}
//	$('#westTD').datagrid({
//		loadFilter : pagerFilter
//	}).datagrid('loadData', getData());
}
function createDV(str){
	var inDvHtml="";
	var json=str.DV.EO;
	var searchAction=str.SERVICE;
	var dateBoxStr="";
	var comboMap=new Map();
	inDvHtml+="<form action="+searchAction+">";
	var ids="";
	for(var i=0;i<json.length;i++){
		if(i%3==0){
			inDvHtml+="<tr>";
		}
			if(json[i].TYPE=="text"){
				inDvHtml+="<td>"+json[i].ALIAS+"： <input id="+json[i].DATA_NAME+" class='easyui-textbox' name="+json[i].EO_NAME+"."+json[i].DATA_NAME+"  data-options="+"iconCls:'icon-search'"+" style='width:"+json[i].WIDTH+"'/></td>"
			}
			if(json[i].TYPE=="dateBox"){
				inDvHtml+="<td>"+json[i].ALIAS+"： <input id="+json[i].DATA_NAME+"  name="+json[i].EO_NAME+"."+json[i].DATA_NAME+"   data-options="+"iconCls:'icon-search'"+" style='"+json[i].WIDTH+"'/></td>";
				dateBoxStr+=json[i].DATA_NAME+",";
			}
			if(json[i].TYPE=="combobox"){
				inDvHtml+="<td>"+json[i].ALIAS+"： <input id="+json[i].DATA_NAME+"  name="+json[i].EO_NAME+"."+json[i].DATA_NAME+"   data-options="+"iconCls:'icon-search'"+" style='"+json[i].WIDTH+"'/></td>"
				comboMap.put(json[i].DATA_NAME,json[i]);
			}
			ids+=json[i].EO_NAME+"."+json[i].DATA_NAME+",";
			
	}
	ids=ids.substring(0,ids.length-1);
	inDvHtml+="<input type='hidden' id='eoName' name='eoName' value="+json[0].EO_NAME+"></input>";
	inDvHtml+="<input type='button' onclick=\"queryToGrid('"+searchAction+"','"+ids+"')\" value='查询'/>";
	inDvHtml+="</table></form>";
	$('#dv').html(inDvHtml);
	//将所有日期类型的生成
	dateBoxStr=dateBoxStr.substring(0,dateBoxStr.length-1);
	dateBoxList=dateBoxStr.split(",");
	for(var i=0;i<dateBoxList.length;i++){
		setDateBox(dateBoxList[i]);
	}
	//将下拉列表生成
	for(var v in comboMap.keys()){
		setSelect(comboMap.element(v).value)
	}
}
function queryToGrid(str,str2){
//	var temp=new Array();
	var strlist=str2.split(",");
	var paramlist;

	var temp1={};
	temp1["paramslist"]=str2;
	for(i=0;i<strlist.length;i++){
		console.log($("input[name='"+strlist[i]+"']").val());
		temp1[strlist[i]]=$("input[name='"+strlist[i]+"']").val();
	}
	
	params=temp1;
	$('#centertable').datagrid("load",params);
}
function setDateBox(id){
	$('#'+id+'').datebox({ 
			formatter:function(date){
			//	alert(date.getfullYear());
				var y = date.getFullYear();
				var m = date.getMonth()+1;
				var d = date.getDate();
				return y+'-'+m+'-'+d;
			},
		parser:function(date){
			var t = Date.parse(date);
			if (!isNaN(t)){
				return new Date(t);
			} else {
				return new Date();
			}

		},
	    required:true   
	});  
}


function setSelect(json){
	var id=json.name;
	$('#'+id+'').combobox({ 
		valueField:"value",
		textField:"title",
		data:json.comboboxvalue
	});
}

function clearForm(){
	
}

$(function() {
//	$(window).resize(function(){
//		$('#centerhtm').datagrid('resize',{
//			width:function(){return document.body.clientWidth;},
//			height:function(){return document.body.clientHeight}
//			
//		})
//	})
	
	var str=getUIJson();
	createDV(str);
	createTV(str);
	//$()

});

//function getData() {
//var rows = [];
//for (var i = 1; i <= 800; i++) {
//	var amount = Math.floor(Math.random() * 1000);
//	var price = Math.floor(Math.random() * 1000);
//	rows.push({
//		id : 'Inv No ' + i,
//		date : $.fn.datebox.defaults.formatter(new Date()),
//		name : 'Name ' + i,
//		sex : amount,
//		home : price
//	});
//}
//return rows;
//}

//function pagerFilter(data) {
//if (typeof data.length == 'number' && typeof data.splice == 'function') { // is
//	// array
//	data = {
//		total : data.length,
//		rows : data
//	}
//}
//var dg = $(this);
//var opts = dg.datagrid('options');
//var pager = dg.datagrid('getPager');
//pager.pagination({
//	onSelectPage : function(pageNum, pageSize) {
//		opts.pageNumber = pageNum;
//		opts.pageSize = pageSize;
//		pager.pagination('refresh', {
//			pageNumber : pageNum,
//			pageSize : pageSize
//		});
//		dg.datagrid('loadData', data);
//	}
//});
//if (!data.originalRows) {
//	data.originalRows = (data.rows);
//}
//var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
//var end = start + parseInt(opts.pageSize);
//data.rows = (data.originalRows.slice(start, end));
//return data;
//} 